﻿using DGSFramework;
using DGSFramework.Models;
using System;
using System.Collections.Generic;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Framework.Resources
{
    /// <summary>
    /// Summary description for GetStock
    /// </summary>
    public class GetStock : IHttpHandler
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            var status = 0;            
            try
            {
                var stock = HttpContext.Current.Request.Form["stock"].ToString();
                var stockList = entity.dgs_StockDatas.Where(d => d.dgs_Stocks.Code.ToUpper() == stock.ToUpper()).Select(d => new[]
                {
                    d.GetTime.Value.ToString("yyyy-MM-dd"),
                    d.GiaThamChieu.ToString(),
                    d.Cao.ToString(),
                    d.Thap.ToString(),
                    d.KL_KL.ToString(),
                    d.KL_KL.ToString()

                    //NDate=d.GetTime.Value.ToString("yyyy-MM-dd"),
                    //Open=d.GiaThamChieu,
                    //Hight=d.Cao,
                    //Low=d.Thap,
                    //Close=d.KL_Gia,
                    //Volume=d.KL_KL
                });
                status = 1;
                context.Response.Write(
                jsonSerializer.Serialize(
                    new
                    {
                        Status = status,
                        Data = stockList
                    }
                )
            );
            }
            catch (Exception ex)
            {
                status = -1;
                context.Response.Write(
                jsonSerializer.Serialize(
                    new
                    {
                        Status = status
                    }
                ));
            }

            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}